package com.jbp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jbp.dao.UnionUserDao;
import com.jbp.exception.RRException;
import com.jbp.model.Unioner;
import com.jbp.model.UnionUser;
import com.jbp.model.User;
import com.jbp.model.UserNobility;
import com.jbp.service.UnionService;
import com.jbp.service.UnionUserService;
import com.jbp.service.UserService;
import com.jbp.utils.StrToMillsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 工会用户(UnionUser)表服务实现类
 * @author cc
 * @since 2021-08-05 20:48:19
 */
@Service
public class UnionUserServiceImpl extends ServiceImpl<UnionUserDao, UnionUser> implements UnionUserService {
    @Autowired
    private UnionService unionService;
    @Autowired
    private UserService userService;
    @Override
    public UnionUser getUnionByUUid(Integer userId, Integer unionUuid) {
        Unioner unioner = unionService.getByUuid(unionUuid);
        if (unioner ==null) {
            throw new RRException("工会不存在");
        }
        UnionUser unionUser = getByUnionId(userId, unioner.getId());
        if (unionUser==null) {
            unionUser = new UnionUser();
            unionUser.setUnionId(unioner.getId());
        }
        unionUser.setCreateTime(unioner.getCreateTime());
        formatUnionUser(unionUser);
        return unionUser;
    }

    @Override
    public UnionUser getByUnionId(Integer userId, Integer unionId) {
        QueryWrapper<UnionUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        wrapper.eq("union_id", unionId);
        return getOne(wrapper);
    }

    @Override
    public void apply(Integer userId, Integer unionId) {
        assertUnionId(unionId);

        QueryWrapper<UnionUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        //0无关，1申请加入，2 加入成功  3加入失败 4退出申请 5退出成功
        wrapper.in("status", 1, 2, 4);
        UnionUser result = getOne(wrapper);
        if (result!=null) {
            throw new RRException("只能申请一个工会");
        }

        UnionUser unionUser = getByUnionId(userId, unionId);
        if (unionUser!=null) {
            //0无关，1申请加入，2 加入成功  3加入失败 4退出申请 5退出成功
            switch (unionUser.getStatus()) {
                case 1:
                    throw new RRException("申请已提交");
                case 2:
                    throw new RRException("已加入该工会");
                case 4:
                    throw new RRException("已提交退出申请");
            }
            unionUser.setStatus(1);
            unionUser.setUpdateTime(new Date());
            updateById(unionUser);
        } else {
            UnionUser one = new UnionUser();
            one.setUnionId(unionId);
            one.setUserId(userId);
            one.setStatus(1);
            one.setUpdateTime(new Date());
            save(one);
        }
    }

    @Override
    public void quit(Integer userId, Integer unionId) {
        assertUnionId(unionId);

        UnionUser unionUser = getByUnionId(userId, unionId);
        if (unionUser!=null) {
            //0无关，1申请加入，2 加入成功  3加入失败 4退出申请 5退出成功
            if (unionUser.getStatus().intValue()!=2) {
                throw new RRException("非工会成员");
            }
            unionUser.setStatus(4);
            unionUser.setUpdateTime(new Date());
            updateById(unionUser);
        } else {
            throw new RRException("未加入该工会");
        }
    }

    public void assertUnionId(Integer unionId) {
        Unioner unioner = unionService.getById(unionId);
        if (unioner ==null) {
            throw new RRException("工会不存在");
        }
    }

    @Override
    public void cacleApply(Integer userId, Integer unionId) {
        assertUnionId(unionId);

        UnionUser unionUser = getByUnionId(userId, unionId);
        if (unionUser!=null) {
            //0无关，1申请加入，2 加入成功  3加入失败 4退出申请 5退出成功
            if (unionUser.getStatus().intValue()!=1) {
                throw new RRException("状态错误");
            }
            unionUser.setStatus(0);
            unionUser.setUpdateTime(new Date());
            updateById(unionUser);
        } else {
            throw new RRException("状态错误");
        }
    }

    @Override
    public void cancleQuit(Integer userId, Integer unionId) {
        assertUnionId(unionId);

        UnionUser unionUser = getByUnionId(userId, unionId);
        if (unionUser!=null) {
            //0无关，1申请加入，2 加入成功  3加入失败 4退出申请 5退出成功
            if (unionUser.getStatus().intValue()!=4) {
                throw new RRException("状态错误");
            }
            unionUser.setStatus(2);
            unionUser.setUpdateTime(new Date());
            updateById(unionUser);
        } else {
            throw new RRException("状态错误");
        }
    }

    @Override
    public UnionUser myUnion(Integer userId) {
        QueryWrapper<UnionUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        //0无关，1申请加入，2 加入成功  3加入失败 4退出申请 5退出成功
        wrapper.in("status", 1, 2, 4);
        UnionUser unionUser = getOne(wrapper);

        return formatUnionUser(unionUser);
    }

    @Override
    public void autoQuitUnion() {
        //申请退出后 5天自动退出
//        QueryWrapper<UnionUser> queryWrapper = new QueryWrapper<>();
//        queryWrapper.apply("UNIX_TIMESTAMP(end_time) < UNIX_TIMESTAMP('" + StrToMillsUtil.dateToStr(new Date()) + "')");
//        queryWrapper.gt("nobility", 2);
//        List<UserNobility> list = list(queryWrapper);
    }

    private UnionUser formatUnionUser(UnionUser unionUser){
        if (unionUser==null) {
            return null;
        }

        Unioner unioner = unionService.getById(unionUser.getUnionId());
        unionUser.setUnionUuid(unioner.getUuid());
        unionUser.setUnionName(unioner.getName());
        unionUser.setUnionImg(unioner.getImg());

        User host = userService.getById(unioner.getUserId());
        unionUser.setUnionHostId(host.getId());
        unionUser.setUnionHostUuid(host.getUuid());
        unionUser.setUnionHostName(host.getName());
        unionUser.setUnionHostImg(host.getImg());

        return unionUser;
    }
}